package Demo16_30.Test30;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;

/**
 * Created by 14229 on 2020/1/31.
 */
public class Util {
    public static boolean login() throws IOException {

        Scanner sc= new Scanner(System.in);
        System.out.println("请输入用户名：");
        String user = sc.nextLine();
        System.out.println("请输入密码:");
        String pasd = sc.nextLine();

        Properties pro = new Properties();

        pro.load(new FileReader("user.properties"));

        String flag = null ;

        for (Object o : pro.keySet()) {
            String s = String.valueOf(o);
            String s1 = String.valueOf(pro.get(o));
            if(s.equals(user) && s1.equals(pasd)){
                flag=s;
            }
        }

        boolean a ;
        if(flag!=null){
            Date d = new Date();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String format = df.format(d);

            String s = flag+" "+format+" 登陆成功！";
            System.out.println("登陆成功！");
            a=true;
            FileUtils.writeStringToFile(new File("data30.txt"),s);
            System.exit(0);
        }else {
            Date d = new Date();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String format = df.format(d);

            String s = user+" "+format+" 登陆失败！";
            System.out.println("登录失败");
            a=false;
            FileUtils.writeStringToFile(new File("data30.txt"),s);
        }

        return a;
    }

    public static void otherLogin() throws IOException {
        Date d = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = df.format(d);

        String s = "游客"+" "+format+" 进行浏览！";


        FileUtils.readFileToString(new File("data30.txt"),s);
        System.exit(0);
    }
}
